package com.fable.biz.operate.impl;

import com.fable.biz.operate.CustomerTypeService;
import com.fable.biz.operate.dto.CustomerTypeDTO;
import com.fable.biz.operate.query.CustomerTypeQueryCondition;
import com.fable.dal.mapper.CustomerTypeMapper;
import com.fable.dal.model.CustomerTypeDO;
import com.fable.dal.model.CustomerTypeQuery;
import com.fable.utils.CalendarUtil;
import com.fable.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName CustomerTypeServiceImpl
 * @Description TODO
 * @Author aaters
 * @Date 2018/7/13 下午5:07
 * @Version 1.0
 **/
@Service("customerTypeService")
public class CustomerTypeServiceImpl implements CustomerTypeService {

    private static final Logger LOG = LoggerFactory.getLogger(CustomerTypeServiceImpl.class);

    @Autowired
    private CustomerTypeMapper customerTypeMapper;


    @Override
    public List<CustomerTypeDTO> queryCustomerTypeList(CustomerTypeQueryCondition condition) {
        try {
            CustomerTypeQuery customerTypeQuery = new CustomerTypeQuery();
            CustomerTypeQuery.Criteria queryCriteria = customerTypeQuery.createCriteria();
            if (condition != null) {
                if (StringUtil.isNotBlank(condition.getTypeName())) {
                    queryCriteria.andTypeNameLike("%" + condition.getTypeName() + "%");
                }
                if (StringUtil.isNotBlank(condition.getStartTime())) {
                    String startTime = condition.getStartTime() + " 00:00:00";
                    Date time = CalendarUtil.parseString2Date(startTime, CalendarUtil.TIME_PATTERN);
                    queryCriteria.andGmtCreateGreaterThanOrEqualTo(time);
                }
                if (StringUtil.isNotBlank(condition.getEndTime())) {
                    String endTime = condition.getEndTime() + " 23:59:59";
                    Date time = CalendarUtil.parseString2Date(endTime, CalendarUtil.TIME_PATTERN);
                    queryCriteria.andGmtCreateLessThanOrEqualTo(time);
                }
            }
            customerTypeQuery.setOrderByClause("id desc ");
            List<CustomerTypeDO> list = customerTypeMapper.selectByQuery(customerTypeQuery);
            List<CustomerTypeDTO> doList = new ArrayList<>();

            list.stream().filter(Objects::nonNull).forEach(customerTypeDO -> {
                CustomerTypeDTO customerTypeDTO = new CustomerTypeDTO();
                BeanUtils.copyProperties(customerTypeDO, customerTypeDTO);
                doList.add(customerTypeDTO);
            });

            LOG.info("获取客户类型成功:{}", doList);
            return doList;
        } catch (Exception e) {
            LOG.info("获取客户类型失败:{}", e.getLocalizedMessage());
        }
        return null;
    }

    @Override
    public CustomerTypeDO queryCustomerById(Integer id) {
        return customerTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addOrModifyCustomerType(CustomerTypeDTO customerTypeDTO) {
        int count = 0;
        try {
            CustomerTypeDO customerTypeDO = customerTypeMapper.selectByPrimaryKey(customerTypeDTO.getId());
            if (customerTypeDO == null) {
                customerTypeDO = new CustomerTypeDO();
                customerTypeDO.setTypeName(customerTypeDTO.getTypeName());
                customerTypeDO.setTypeDescribe(customerTypeDTO.getTypeDescribe());
                customerTypeDO.setGmtCreate(new Date());
                customerTypeDO.setGmtModified(new Date());
                count = customerTypeMapper.insertSelective(customerTypeDO);
            } else {
                BeanUtils.copyProperties(customerTypeDTO, customerTypeDO);
                customerTypeDO.setGmtModified(new Date());
                count = customerTypeMapper.updateByPrimaryKeySelective(customerTypeDO);
            }
        } catch (Exception e) {
            LOG.info("添加或修改客户类型失败:{}", e.getLocalizedMessage());
        }
        return count;
    }

    @Override
    public int delCustomerType(CustomerTypeDTO customerTypeDTO) {
        int count = 0;
        try {
            if (customerTypeDTO != null) {
                CustomerTypeDO customerTypeDO = new CustomerTypeDO();
                BeanUtils.copyProperties(customerTypeDTO, customerTypeDO);
                count = customerTypeMapper.updateByPrimaryKeySelective(customerTypeDO);
            }
            LOG.info("逻辑删除客户类型成功:" + customerTypeDTO.getId());
        } catch (Exception e) {
            LOG.error("逻辑删除客户类型失败:{}", e.getLocalizedMessage());
        }
        return count;
    }
}
